﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace _001_TXT_小说爬虫
{
    public class HttpMethod
    {
        public static string HttpPost(string url, string postDataStr)
        {
            //请求内容
            CookieContainer cookie = new CookieContainer();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
            request.CookieContainer = cookie;
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();
            //发送请求
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //获取返回结果
            response.Cookies = cookie.GetCookies(response.ResponseUri);
            Stream myResponseStream = response.GetResponseStream();
            //gzip解压缩
            if (response.ContentEncoding.ToLower() == "gzip")
            {
                myResponseStream = new System.IO.Compression.GZipStream(myResponseStream, System.IO.Compression.CompressionMode.Decompress);
            }
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string responseStr = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return responseStr;
        }

        public static string HttpGet(string url, string getDataStr = "")
        {
            string newUrl = url + (getDataStr == "" ? "" : ("?" + getDataStr));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newUrl);
            request.Method = "GET";
            request.ContentType = "text/html; charset=gbk";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36";

            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException)
            {
                response = (HttpWebResponse)request.GetResponse();
                Console.WriteLine("请求出现异常:" + newUrl);
            }

            Stream myResponseStream = response.GetResponseStream();
            //gzip解压缩
            if (response.ContentEncoding.ToLower() == "gzip")
            {
                myResponseStream = new System.IO.Compression.GZipStream(myResponseStream, System.IO.Compression.CompressionMode.Decompress);
            }

            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gbk"));
            string responseStr = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return responseStr;
        }
    }
}
